<?php

namespace App\Admin\Controllers;

use App\Models\School;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Support\MessageBag;

class SchoolController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return redirect('sm/school/1/edit');
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('学校管理')
            ->description('学校详情信息')
            ->body($this->detail($id));

    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('学校管理')
            ->description('编辑学校信息')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('学校管理')
            ->description('新建学校信息')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new School);

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(School::findOrFail($id));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new School());

        $form->text('code', trans('学校唯一标识码'))->rules('required', [
            'required' => '学校唯一标识码不能为空'
        ])->setWidth(4, 2);
        $form->text('name', trans('学校名称'))->rules('required', [
            'required' => '学校名称不能为空'
        ])->setWidth(4, 2);
        $form->text('address', trans('学校地址'))->rules('required', [
            'required' => '学校地址不能为空'
        ])->setWidth(4, 2);
        $form->select('demonstrine', trans('示范校建设情况'))->options(['0' => '其他', '1' => '国家示范校', '2' => '国家骨干校', '3' => '省示范校'])->setWidth(4, 2);
        $form->select('type', trans('学校类型'))->options(['0' => '其他', '1' => '部署', '2' => '省教育厅公办', '3' => '省属其他部门公办' , '4' => '地方政府公办' , '5' => '民办'])->setWidth(4, 2);
        $form->date('creat', '建校日期');
        $form->editor('intro', '教育格言');

        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });
        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });
        $form->saved(function () {
            return back()->with(admin_toastr('保存成功...', 'success'));
        });
        return $form;
    }
}
